package com.alibaba.bos.action.base;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Join;
import javax.persistence.criteria.JoinType;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.apache.commons.lang3.StringUtils;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.hibernate.mapping.Array;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Controller;

import com.alibaba.bos.domain.base.Courier;
import com.alibaba.bos.service.base.CourierService;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

@Controller
@ParentPackage("json-default")
@Namespace("/")
@Scope("prototype")
public class CourierAction extends ActionSupport implements ModelDriven<Courier> {

    private Courier courier = new Courier();

    @Override
    public Courier getModel() {
        return courier;
    }

    @Autowired
    private CourierService courierService;

    // 添加派件员信息
    @Action(value = "courier_save", results = {
            @Result(name = "success", type = "redirect", location = "./pages/base/courier.html") })
    public String save() {
        courierService.save(courier);
        return SUCCESS;
    }

    // 属性封装
    private int page;
    private int rows;

    public void setPage(int page) {
        this.page = page;
    }

    public void setRows(int rows) {
        this.rows = rows;
    }

    // 分页查询派件员
    @Action(value = "courier_pageQuery", results = { @Result(name = "success", type = "json") })
    public String pageQuery() {
        Pageable pageable = new PageRequest(page - 1, rows);
        Specification<Courier> specification = new Specification<Courier>() {

            @Override
            public Predicate toPredicate(Root<Courier> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                // Predicate predicate = cb.isNotNull(root.get("id"));
                List<Predicate> list = new ArrayList<>();
                // 简单单表查询
                // 工号
                if (StringUtils.isNotBlank(courier.getCourierNum())) {
                    Predicate p1 = cb.equal(root.get("courierNum").as(String.class), courier.getCourierNum());
                    list.add(p1);
                }
                // 所属公司查询
                if (StringUtils.isNotBlank(courier.getCompany())) {
                    Predicate p2 = cb.like(root.get("company").as(String.class), "%" + courier.getCompany() + "%");
                    list.add(p2);
                }
                // 员工类型查询
                if (StringUtils.isNotBlank(courier.getType())) {
                    Predicate p3 = cb.like(root.get("type").as(String.class), "%" + courier.getType() + "%");
                    list.add(p3);
                }
                // 复杂多表查询
                // 根据取派标准查询
                Join<Object, Object> standardRoot = root.join("standard", JoinType.INNER);
                if (courier.getStandard() != null && StringUtils.isNotBlank(courier.getStandard().getName())) {
                    Predicate p4 = cb.like(standardRoot.get("name").as(String.class),
                            "%" + courier.getStandard().getName() + "%");
                    list.add(p4);
                }

                return cb.and(list.toArray(new Predicate[0]));
            }
        };
        Page<Courier> page = courierService.findPageData(specification, pageable);
        // 转为需要的格式
        Map<String, Object> pages = new HashMap<>();
        pages.put("rows", page.getContent());
        pages.put("total", page.getTotalElements());

        // 将数据存入值栈
        ActionContext.getContext().getValueStack().push(pages);
        return SUCCESS;
    }

    // 获取传递过来的参数，可以通过属性驱动
    private String ids;

    public void setIds(String ids) {
        this.ids = ids;
    }

    // 批量操作快递员
    @Action(value = "courier_delBatch", results = {
            @Result(name = "success", location = "./pages/base/courier.html", type = "redirect") })
    public String delBatch() {
        // 根据逗号截取字符串
        String[] id = ids.split(",");
        courierService.delBatch(id);
        return SUCCESS;
    }
}
